using System;
using System.Data;
using System.Globalization;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using eStore.Business;
using eStore.Helpers;
using Resources;
using System.Collections;
using mojoPortal.Business;

namespace eStore.UI
{
    public partial class AdminOrderHistoryPage : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private string pageNumberParam;
        //private string sortParam;
        private int pageNumber = 1;
        private const int pageSize = 30;
        private int totalPages = 1;

        private Store store;
        protected Double timeOffset;
        protected TimeZoneInfo timeZone;
        protected CultureInfo currencyCulture = CultureInfo.CurrentCulture;
        protected string detailUrlFormat = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.SslIsAvailable()) SiteUtils.ForceSsl();

            if (!UserCanEditModule(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            LoadSettings();

            SetupCss();
            PopulateLabels();
            PopulateControls();

        }

        private void PopulateControls()
        {
            if (Page.IsPostBack) return;

            BindGrid();

        }

        private void BindGrid()
        {
            if (store == null) return;

            using (IDataReader reader = Order.GetPage(
                store.Guid,
                pageNumber,
                pageSize,
                out totalPages))
            {
                string pageUrl = SiteUtils.GetNavigationSiteRoot() + "/eStore/AdminOrderHistory.aspx"
                    + "?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "&amp;pagenumber" + moduleId.ToInvariantString()
                    + "={0}";

                pgrOrders.PageURLFormat = pageUrl;
                pgrOrders.ShowFirstLast = true;
                pgrOrders.CurrentIndex = pageNumber;
                pgrOrders.PageSize = pageSize;
                pgrOrders.PageCount = totalPages;
                pgrOrders.Visible = (totalPages > 1);

                grdOrders.DataSource = reader;
                grdOrders.PageIndex = pageNumber;
                grdOrders.PageSize = pageSize;
                grdOrders.DataBind();
            }

        }

        protected string GetOrderStatus(string sGuid)
        {
            if (string.IsNullOrEmpty(sGuid)) { return eStoreResources.OrderStatusNone; }

            if (sGuid.Length != 36) { return eStoreResources.OrderStatusNone; }

            Guid statusGuid = new Guid(sGuid);
            return StoreHelper.GetOrderStatusName(statusGuid);

        }


        private void PopulateLabels()
        {

            lnkStoreMenu.Text = eStoreResources.StoreManagerHeading;
            lnkStoreMenu.NavigateUrl = SiteRoot + "/eStore/AdminDashboard.aspx?pageid="
                                       + pageId.ToString(CultureInfo.InvariantCulture)
                                       + "&mid=" + moduleId.ToString(CultureInfo.InvariantCulture);

            lnkCurrentPage.Text = eStoreResources.OrderHistoryAdminLink;
            lnkCurrentPage.NavigateUrl = Request.Url.ToString();

            ltrTitle.Text = eStoreResources.OrderHistoryAdminLink;
            Title = SiteUtils.FormatPageTitle(siteSettings, eStoreResources.OrderHistoryAdminLink);
            grdOrders.Columns[0].HeaderText = eStoreResources.OrderHistoryCustomerHeading;
            grdOrders.Columns[1].HeaderText = eStoreResources.CartOrderInfoCustomerEmailLabel;
            grdOrders.Columns[2].HeaderText = eStoreResources.OrderHistoryOrderTotalHeader;
            grdOrders.Columns[3].HeaderText = eStoreResources.OrderHistoryDateHeader;
            grdOrders.Columns[4].HeaderText = eStoreResources.OrderStatusLabel;
        }

        private void LoadSettings()
        {

            if (CurrentPage.ContainsModule(moduleId))
                store = StoreHelper.GetStore();

            pageNumberParam = "pagenumber" + moduleId.ToString(CultureInfo.InvariantCulture);
            pageNumber = WebUtils.ParseInt32FromQueryString(pageNumberParam, 1);

            timeOffset = SiteUtils.GetUserTimeOffset();
            timeZone = SiteUtils.GetUserTimeZone();

            detailUrlFormat = SiteUtils.GetNavigationSiteRoot() + "/eStore/AdminOrderDetail.aspx"
                + "?pageid=" + pageId.ToString(CultureInfo.InvariantCulture)
                + "&amp;mid=" + moduleId.ToString(CultureInfo.InvariantCulture)
                + "&order={0}";

            //if (store == null) { return; }

            //currencyCulture = ResourceHelper.GetCurrencyCulture(store.DefaultCurrency);
            currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code);

            //if ((store != null)&&(store.CurrencyFormatString.Length > 0)) 
            //    currencyFormat = store.CurrencyFormatString;

        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            UserCanEditModule(moduleId);

        }

        protected virtual void SetupCss()
        {
            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("styleestore") != null) return;
            Literal cssLink = new Literal
                                  {
                                      ID = "styleestore",
                                      Text = @"<link href='"
                                             + SiteUtils.GetSkinBaseUrl()
                                             + @"styleestore.css' type='text/css' rel='stylesheet' media='screen' />"
                                  };

            stylesheet.Controls.Add(cssLink);
        }
        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            LoadSideContent(false, false, true, true, 0);
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            Load += Page_Load;

            SuppressPageMenu();
        }

        #endregion

    }
}
